﻿using e_commerce.BLL.Data;
using e_commerce.BLL.Services.Enterprise;
using System;
using System.Collections.Generic;
using System.Linq;

namespace e_commerce.BLL
{
    public class AccountService : IDisposable
    {
        private commerceDbContext _dbContext;

        public AccountService()
        {
            _dbContext = new commerceDbContext();
        }
        private Employee GetEmployeeById(string account, string password)
        {
            var employees = _dbContext.User.ToList();
            Employee employee = _dbContext.Employee.Include("User").SingleOrDefault(e => e.User.Account == account && e.User.Password == password);
            if (employee == null)
            {
                throw new Exception("未查询到该管理员");
            }
            return employee;
        }

        public Employee EmployeeLogin(string account, string password)
        {
            var employee = GetEmployeeById(account, password);
            RecordInfoToAdminLog("管理员登录", employee.Id);
            return employee;
        }

        public void RecordInfoToAdminLog(string operatDetail, int empolyeeId)
        {
            AdminLog log = new AdminLog()
            {
                EmployeeId = empolyeeId,
                OperationTime = DateTime.Now,
                OperatDetail = operatDetail,
                Operater = _dbContext.Employee.SingleOrDefault(e => e.Id == empolyeeId).User.NickName,
            };
            _dbContext.AdminLog.Add(log);
            SaveChanges();
        }

        public void RecordInfoToClientLog(string operatDetail, int clientId)
        {
            AdminLog log = new AdminLog()
            {
                EmployeeId = clientId,
                OperationTime = DateTime.Now,
                OperatDetail = operatDetail,
                Operater = _dbContext.Employee.SingleOrDefault(e => e.Id == clientId).User.NickName,
            };
            _dbContext.AdminLog.Add(log);
            SaveChanges();
        }

        public User GetUserById(int userId)
        {
            var user = _dbContext.User.SingleOrDefault(u => u.UserId == userId);
            if(user == null)
            {
                throw new Exception("用户不存在");
            }
            return user;
        }
        private Client GetClient(string account, string password)
        {
            Client client = _dbContext.Client.Include("User")
                .SingleOrDefault(c => c.User.Account == account && c.User.Password == password);
            if (client == null)
            {
                throw new Exception("未找到该用户");
            }
            return client;
        }

        public Client GetClient(int clientId)
        {
            var client = _dbContext.Client.Include("User")
                .SingleOrDefault(c => c.ClientId == clientId);
            if (client == null)
            {
                throw new Exception("未找到该用户");
            }
            return client;
        }
        public Client ClientLogin(string account, string password)
        {
            var client = GetClient(account, password);
            RecordInfoToClientLog("用户登录", client.ClientId);
            return client;
        }
        public void RegisterClient(string account,string password,string email)
        {
            User user = CreateUser( account ,password,email);
            Client client = new Client()
            {
                UserId = user.UserId,
                User = user,
            };
            _dbContext.Client.Add(client);
            SaveChanges();
        }

        public User CreateUser(string account, string password, string email)
        {
            if (!CheckAccount(account))
            {
                throw new Exception("用户已存在");
            }
            User user = new User()
            {
                Account = account,
                NickName = account,
                Password = password,
                Email = email,
                CreateTime = DateTime.Now,
            };
            _dbContext.User.Add(user);
            SaveChanges();
            return user;
        }

        public User CreateUser(User user)
        {
            if (!CheckAccount(user.Account))
            {
                throw new Exception("用户已存在");
            }
            user.CreateTime = DateTime.Now;
            _dbContext.User.Add(user);
            SaveChanges();
            return user;
        }

        public Boolean CheckAccount(string account)
        {
            var exitAccount = _dbContext.User.SingleOrDefault(u => u.Account == account);
            return exitAccount == null?true:false;
        }
        public void SaveChanges()
        {
            _dbContext.SaveChanges();
        }

        public void Dispose()
        {
            _dbContext.Dispose();
        }
    }
}
